//
//  DeviceScanResultViewController.m
//  sentorise
//
//  Created by admin on 2025/8/25.
//

#import "DeviceScanResultViewController.h"
#import "GlobalDefine.h"
#import "DeviceConnectCheckView.h"
#import "ProgressHUD.h"
#import "DeviceBarcodeScanViewController.h"
#import "BlutoothGroupModel.h"
#import "DeviceDataInfoViewController.h"
#import "HelpCenterViewController.h"

@interface DeviceScanResultViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;

@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet UILabel *tipsLabel;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *helpLabel;

@property (weak, nonatomic) IBOutlet UILabel *tipsLabel1;
@property (weak, nonatomic) IBOutlet UILabel *tipsLabel2;

@end

@implementation DeviceScanResultViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.topSpace.constant = IphoneX ? 54 : 20;
    self.bgView.layer.borderWidth = 2;
    self.bgView.layer.borderColor = [UIColor blackColor].CGColor;
    
    self.titleLabel.text = NSLocalizedString(@"RV Battery", nil);
    self.helpLabel.text = NSLocalizedString(@"Need Help", nil);
    self.tipsLabel1.text = NSLocalizedString(@": For first use, please fully charge the battery", nil);
    self.tipsLabel2.text = NSLocalizedString(@"Tips", nil);
    self.descLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Please enter the\n%@ code\nof the connected device", nil), self.inputDown ? @"MAC" : @"SN"];
    self.codeTF.placeholder = self.inputDown ? @"MAC:E1:F1:E1:F1" : @"SN:LTUY-83SF-PIKH-12";
    self.tipsLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Tip: The sticker with printed %@ code is posted on the equipment or packaging carton", nil), self.inputDown ? @"MAC" : @"SN"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewDetail:) name:@"AddSuccessViewDetail" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(restartView) name:@"AddRestartView" object:nil];
}

- (void)viewDetail:(NSNotification *)nf {
    [self.navigationController popToRootViewControllerAnimated:NO];
    if (nf.object) {
        [BTManager sharedBTManager].isGettingBattery = NO;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            UINavigationController *nv = (UINavigationController *)[UIApplication sharedApplication].delegate.window.rootViewController;
            DeviceDataInfoViewController *vc = [[DeviceDataInfoViewController alloc] initWithNibName:@"DeviceDataInfoViewController" bundle:nil];
            vc.model = nf.object;
            [nv pushViewController:vc animated:YES];
        });
    }
}

- (IBAction)actionBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)restartView {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)actionHelp:(id)sender {
    HelpCenterViewController *vc = [[HelpCenterViewController alloc] initWithNibName:@"HelpCenterViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)actionScan:(id)sender {
    if (self.inputDown) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    DeviceBarcodeScanViewController *vc = [[DeviceBarcodeScanViewController alloc] initWithNibName:@"DeviceBarcodeScanViewController" bundle:nil];
    WeakSelf
    vc.scanDown = ^(NSString *code) {
        weakSelf.codeTF.text = code;
    };
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)actionConfirm:(id)sender {
    [self.codeTF endEditing:YES];
    if (!self.codeTF.text.length) {
        [ProgressHUD showErrorMessage:[NSString stringWithFormat:NSLocalizedString(@"Please enter %@", nil), self.inputDown ? @"mac" : @"sn"]];
        return;
    }
    if (self.inputDown) {
        self.inputDown(self.codeTF.text);
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    NSString *listStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"BatteryBluList"] ?: @"";
    NSMutableArray *arr = [NSMutableArray array];
    if (listStr.length) {
        NSArray *dataArray = [BlutoothModel covertToModelList:listStr];
        [arr addObjectsFromArray:dataArray];
    }
    for (BlutoothModel *model in arr) {
        if (([model.mac isEqualToString:self.mac] && self.mac.length) || [model.sn isEqualToString:self.codeTF.text]) {
            [ProgressHUD showErrorMessage:NSLocalizedString(@"The MAC address and SN already exist", nil)];
            return;
        }
    }
    [DeviceConnectCheckView show:self.mac sn:self.codeTF.text type:self.type peripheral:self.peripheral];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return [textField resignFirstResponder];
}

@end
